package com.example.feidaolumanager.adapter

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.amap.api.maps.AMap.InfoWindowAdapter
import com.amap.api.maps.model.Marker
import com.example.feidaolumanager.R
import com.example.feidaolumanager.pojo.MachineItem

class MyMarkerInfoWindowAdapter(private val context: Context) : InfoWindowAdapter {

    private val TAG: String = this.toString()

    lateinit var infoWindow: View

    var currentMarker: Marker? = null

    private var machines: List<MachineItem> = listOf()

    override fun getInfoWindow(marker: Marker?): View {
        currentMarker = marker

        infoWindow = LayoutInflater.from(context).inflate(
            R.layout.custom_info_window, null
        )

        machines.find { it.mec_no == marker?.title }?.let {
            infoWindow.findViewById<TextView>(R.id.machineType).text = it.type_name
            infoWindow.findViewById<TextView>(R.id.electric).text = it.electric
            infoWindow.findViewById<TextView>(R.id.mec_no).text = it.mec_no
            val stateText = infoWindow.findViewById<TextView>(R.id.state)
            when (it.status) {
                0 -> {
                    stateText.text = "静止"
                    stateText.setTextColor(context.getColor(R.color.light_blue_600))
                }

                1 -> {
                    stateText.text = "工作"
                    stateText.setTextColor(context.getColor(R.color.green))
                }

                else -> {
                    stateText.text = "强拆"
                    stateText.setTextColor(context.getColor(R.color.yellow))
                }
            }
            infoWindow.findViewById<TextView>(R.id.position).text = it.formatted_address
            infoWindow.findViewById<TextView>(R.id.pm).text = it.pm25
            infoWindow.findViewById<TextView>(R.id.mobile).text = it.mobile
            infoWindow.findViewById<TextView>(R.id.contacts).text = it.contacts

            infoWindow.findViewById<ImageView>(R.id.closeBtn).setOnClickListener {
                marker?.hideInfoWindow()
            }
        }


        //如果想修改自定义Infow中内容，请通过view找到它并修改


        return infoWindow
    }

    override fun getInfoContents(p0: Marker?): View {
        TODO("Not yet implemented")
    }

    fun setData(machines: List<MachineItem>) {
        Log.e(TAG, "setData: 获得点数据${machines.size}")
        this.machines = machines
    }
}